using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ZTools.ZSingletons;

public class UIManager : ZSingleton<UIManager>
{
    private static Transform _uiRoot;

    // 路径配置字典
    private Dictionary<string, string> pahtDict;

    // 预制件缓存字典
    private Dictionary<string, GameObject> prefabDict;

    // 已打开界面的缓存字典
    public Dictionary<string, BasePanel> panelDict;


    public Transform UIRoot
    {
        get
        {
            if (_uiRoot == null)
            {
                if (GameObject.Find("Canvas"))
                {
                    _uiRoot = GameObject.Find("Canvas").transform;
                }
                else
                {
                    _uiRoot = new GameObject("Canvas").transform;
                }
            }

            return _uiRoot;
        }
    }

    private UIManager()
    {
        InitDicts();
    }

    private void InitDicts()
    {
        prefabDict = new Dictionary<string, GameObject>();
        panelDict = new Dictionary<string, BasePanel>();
        pahtDict = new Dictionary<string, string>()
        {
            { UIConst.PackagePanel, "Package/PackagePanel" },
            { UIConst.DialoguePanel, "Dialogue/DialoguePanel" },
            // {"BagPanel", "UI/BagPanel"},
            // {"BattlePanel", "UI/BattlePanel"},
            // {"MainPanel", "UI/MainPanel"},
            // {"PackagePanel", "UI/PackagePanel"},
            // {"RolePanel", "UI/RolePanel"},
            // {"SkillPanel", "UI/SkillPanel"},
            // {"TipsPanel", "UI/TipsPanel"},
        };
    }

    public BasePanel GetPanel(string name)
    {
        BasePanel panel = null;
        // 检查是否已打开
        if (panelDict.TryGetValue(name, out panel))
        {
            return panel;
        }

        return null;
    }

    public virtual BasePanel OpenPanel(string name)
    {
        BasePanel panel = null;
        // 检查是否已打开
        if (panelDict.TryGetValue(name, out panel))
        {
            Debug.Log($"界面已打开: {name}");
            return panel;
        }

        // 检查路径是否配置
        string path = "";
        if (!pahtDict.TryGetValue(name, out path))
        {
            Debug.Log($"界面名称错误，或未配置路径: {name}");
            return null;
        }

        // 使用缓存预制件
        GameObject panelPrefab = null;
        if (!prefabDict.TryGetValue(name, out panelPrefab))
        {
            string realPath = "Prefabs/UI/" + path;

            Debug.Log($"预制件地址: {realPath}");
            panelPrefab = Resources.Load<GameObject>(realPath);
            prefabDict.Add(name, panelPrefab);
        }

        // 打开界面
        GameObject panelObject = Instantiate(panelPrefab, UIRoot, false);
        panel = panelObject.GetComponent<BasePanel>();
        panelDict.Add(name, panel);
        panel.OpenPanel(name);
        return panel;
    }

    public bool ClosePanel(string name)
    {
        BasePanel panel = null;
        if (!panelDict.TryGetValue(name, out panel))
        {
            Debug.Log($"界面未打开: {name}");
            return false;
        }

        panel.ClosePanel();
        return true;
    }
}

public class UIConst
{
    public const string PackagePanel = "PackagePanel";
    public const string DialoguePanel = "DialoguePanel";
}